<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'sports:sportsWeight:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <!-- <template #firstColumn="{ record }">
        <a @click="handleForm({ indexId: record.indexId })">
          {{ record.indexType }}
        </a>
      </template> -->
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <TableForm @register="registerTableForm" @success="handleSuccess" />
    <suggestionModal @register="suggestionModalVue" @success="handleSuccess" />
    <AdditionalPoints @register="AdditionalPointsTable" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSportsSportsWeightList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { sportsWeightDelete, sportsWeightListData } from '/@/api/sports/sportsWeight';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import TableForm from './table.vue';
  import suggestionModal from './suggestionModal.vue';
  import AdditionalPoints from './AdditionalPoints.vue';
  import { useModal } from '/@/components/Modal';

  const { t } = useI18n('sports.sportsWeight');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('单项指标模块管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('单项指标名称'),
        field: 'indexName',
        component: 'Input',
      },
      {
        label: t('指标分类'),
        field: 'indexType',
        component: 'Select',
        componentProps: {
          dictType: 's_index_type',
          allowClear: true,
        },
      },
      {
        label: t('适配年级'),
        field: 'adaptGrade',
        component: 'Select',
        componentProps: {
          dictType: 'vke_grade',
          allowClear: true,
          mode: 'multiple',
        },
      },
      // {
      //   label: t('单项指标单位'),
      //   field: 'indexUnit',
      //   component: 'Input',
      // },
      // {
      //   label: t('权重'),
      //   field: 'weight',
      //   component: 'Input',
      // },
      // {
      //   label: t('建议'),
      //   field: 'proposal',
      //   component: 'Input',
      // },
      // {
      //   label: t('是否自定义指标'),
      //   field: 'customizeIndex',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 's_index_customize',
      //   },
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('单项指标名称'),
      dataIndex: 'indexName',
      key: 'a.index_name',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('指标分类'),
      dataIndex: 'indexType',
      key: 'a.index_type',
      //sorter: true,
      width: 130,
      align: 'left',
      dictType: 's_index_type',
    },
    {
      title: t('适配年级'),
      dataIndex: 'adaptGrade',
      key: 'a.adapt_grade',
      sorter: true,
      width: 330,
      align: 'left',
      dictType: 'vke_grade',
    },
    {
      title: t('单项指标单位'),
      dataIndex: 'indexUnit',
      key: 'a.index_unit',
      sorter: true,
      width: 110,
      align: 'left',
      dictType: 's_index_unit',
    },
    {
      title: t('权重'),
      dataIndex: 'weight',
      key: 'a.weight',
      //sorter: true,
      width: 60,
      align: 'right',
    },
    {
      title: t('解释'),
      dataIndex: 'proposal',
      key: 'a.proposal',
      //sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('是否自定义'),
      dataIndex: 'customizeIndex',
      key: 'a.customize_index',
      //sorter: true,
      width: 100,
      align: 'center',
      dictType: 's_index_customize',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      //sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 200,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑单项指标模块'),
        onClick: handleForm.bind(this, { indexId: record.indexId }),
        auth: 'sports:sportsWeight:edit',
      },
      {
        icon: 'ant-design:flag-outlined',
        title: t('解释'),
        onClick: suggestion.bind(this, { indexId: record.indexId, proposal: record.proposal }),
        auth: 'sports:sportsWeight:edit',
      },
      {
        icon: 'ant-design:vertical-align-middle-outlined',
        title: t('平均值'),
        onClick: averageForm.bind(this, { indexId: record.indexId, indexName: record.indexName }),
        auth: 'sports:sportsWeight:edit',
      },
      {
        icon: 'ant-design:container-outlined',
        title: t('附加分'),
        //label: '附加分',
        onClick: onAdditionalPoints.bind(this, {
          indexId: record.indexId,
          indexName: record.indexName,
        }),
        auth: 'sports:sportsWeight:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除单项指标模块'),
        popConfirm: {
          title: t('是否确认删除单项指标模块'),
          confirm: handleDelete.bind(this, { indexId: record.indexId }),
        },
        auth: 'sports:sportsWeight:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: sportsWeightListData,
    beforeFetch: (params) => {
      return params;
    },
    afterFetch: (res) => {
      res.forEach((item) => {
        if (item.weight || item.weight == 0) {
          item.weight = item.weight + '%';
        } else {
          item.weight = '--';
        }
      });
      return res;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  const [registerTableForm, { openDrawer: openTable }] = useDrawer();

  const [suggestionModalVue,{ openModal: openSuggestion }] = useModal();

  const [AdditionalPointsTable, { openDrawer: openAdditional }] = useDrawer();

  function averageForm(record: Recordable) {
    openTable(true, record);
  }

  function suggestion(record: Recordable) {
    openSuggestion(true, record);
    handleSuccess();
  }

  function onAdditionalPoints(record: Recordable) {
    openAdditional(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await sportsWeightDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
